package com.reviewit.mbean;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import com.reviewit.dao.AdministratorDao;
import com.reviewit.model.Administrator;

@ManagedBean
@SessionScoped
public class AdministratorBean extends GenericBean implements Serializable,
		NavigationRules {
	private static final long serialVersionUID = 1L;

	private DataModel<Administrator> administrators;
	private Administrator administrator;
	private AdministratorDao dao;

	public AdministratorBean() {
		super();
		this.log = Logger.getLogger(this.getClass().getName());
		this.dao = AdministratorDao.getInstance();
		this.administrator = new Administrator();
	}

	public void login() {

	}

	public void logout() {

	}

	public Administrator getAdministrator() {
		return administrator;
	}

	public void setAdministrator(Administrator administrator) {
		this.administrator = administrator;
	}

	public void setAdministrators(DataModel<Administrator> administrators) {
		this.administrators = administrators;
	}

	public DataModel<Administrator> getAdministrators() {
		List<Administrator> list = dao.find().asList();
		administrators = new ListDataModel<Administrator>(list);
		return administrators;
	}

	public void select() {
		setAdministrator(administrators.getRowData());
	}

	public String findById() {
		Administrator obj = dao.findOne("id", administrator.getId());
		setAdministrator(obj);

		return NavigationRules.FIND_ENTITY;
	}

	public String saveOrUpdate() {
		Administrator obj = dao.findOne("id", administrator.getId());
		if (obj == null) {
			return this.update();
		} else {
			return this.save();
		}
	}

	public String save() {
		try {
			dao.save(administrator);
		} catch (Exception e) {
			return e.getMessage();
		}

		setAdministrator(new Administrator());
		return "Administrator saved!";
	}

	public String update() {
		try {
			dao.save(administrator);
		} catch (Exception e) {
			return e.getMessage();
		}

		setAdministrator(new Administrator());
		return "Administrator updated!";
	}

	public String delete() {
		try {
			dao.delete(administrator);
		} catch (Exception e) {
			return e.getMessage();
		}

		setAdministrator(new Administrator());
		return "Administrator deleted!";
	}

	public String back() {
		return NavigationRules.BACK_CADASTRO;
	}

	public void nnew() {
		setAdministrator(new Administrator());
	}
	
}